<html>
<head><meta charset="utf-8"><title>questions from Stack Overflow · wg-traits · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/index.html">wg-traits</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html">questions from Stack Overflow</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="157298199"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157298199" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157298199">(Jan 31 2019 at 19:27)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">trait</span><span class="w"> </span><span class="n">Works</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>This is does not report any errors from the compiler. If you add a body (even <code>{ unimplemented!() }</code>) it complains about <code>Sized</code>. Is there any possible implementation of this trait method that <em>doesn't</em> require <code>Sized</code>? Should the unimplemented trait method have the same warning?</p>



<a name="157298279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157298279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157298279">(Jan 31 2019 at 19:28)</a>:</h4>
<div class="codehilite"><pre><span></span><span class="k">trait</span><span class="w"> </span><span class="n">Errors</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">foo</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nb">Option</span><span class="o">&lt;</span><span class="n">Self</span><span class="o">&gt;</span><span class="p">;</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>this <em>does</em> have the warning</p>



<a name="157299640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299640">(Jan 31 2019 at 19:41)</a>:</h4>
<p>I think the latter error is more due to a property of <code>Option</code> itself rather than of the trait</p>



<a name="157299643"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299643" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299643">(Jan 31 2019 at 19:41)</a>:</h4>
<p>well</p>



<a name="157299655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299655">(Jan 31 2019 at 19:41)</a>:</h4>
<p>you can't do <code>Option&lt;[u32]&gt;</code> for example</p>



<a name="157299728"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299728" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299728">(Jan 31 2019 at 19:42)</a>:</h4>
<p>so <span class="user-mention" data-user-id="116155">@Jake Goulding</span> we intentionally (but confusingly)  say that "every function <strong>body</strong> checks that its return type is sized"; this was mostly to allow us room to expand traits in the future to permit unsized types in more places</p>



<a name="157299748"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299748" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299748">(Jan 31 2019 at 19:42)</a>:</h4>
<p>notably, we wanted to define</p>
<div class="codehilite"><pre><span></span><span class="k">trait</span><span class="w"> </span><span class="nb">FnOnce</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">call</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="p">...);</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<p>without requiring <code>FnOnce: Sized</code></p>



<a name="157299794"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299794" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299794">(Jan 31 2019 at 19:43)</a>:</h4>
<p>(since we expect that to be implementable for unsized types eventually)</p>



<a name="157299872"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299872" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299872">(Jan 31 2019 at 19:44)</a>:</h4>
<p>the point is, that trait def'n doesn't have a function <strong>body</strong>, so it doesn't enforce the requirement that the parameter types are sized, though it's true that to actually <em>implement</em> the trait, the self type must (presently) be sized</p>



<a name="157299914"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157299914" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157299914">(Jan 31 2019 at 19:44)</a>:</h4>
<p>(what <span class="user-mention" data-user-id="116083">@pnkfelix</span> said re: Option is also true, that's a separate case)</p>



<a name="157300165"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157300165" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157300165">(Jan 31 2019 at 19:47)</a>:</h4>
<p>Ok, let me say that back to you.</p>



<a name="157300322"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157300322" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157300322">(Jan 31 2019 at 19:49)</a>:</h4>
<p>There are two sets of checks happening here, which is why the difference appears confusing. <code>Option</code> requires <code>T: Sized</code>. Something that doesn't require <code>Sized</code> would be fine (e.g. <code>Box&lt;Self&gt;</code>)</p>



<a name="157300467"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157300467" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157300467">(Jan 31 2019 at 19:50)</a>:</h4>
<p>The second check is that a (trait? any?) function <em>with a body</em> requires all param types (including return type?) to be sized.</p>



<a name="157300622"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157300622" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157300622">(Jan 31 2019 at 19:52)</a>:</h4>
<p>I guess only traits can have "bodiless functions".</p>



<a name="157300655"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157300655" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157300655">(Jan 31 2019 at 19:53)</a>:</h4>
<p>Can the "unsized locals/rvalue" work not apply to a return type then?</p>



<a name="157300680"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157300680" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157300680">(Jan 31 2019 at 19:53)</a>:</h4>
<p>Oh:</p>
<div class="codehilite"><pre><span></span>// You CANNOT have unsized return types.
</pre></div>



<a name="157301058"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157301058" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157301058">(Jan 31 2019 at 19:56)</a>:</h4>
<blockquote>
<p>The second check is that a (trait? any?) function <em>with a body</em> requires all param types (including return type?) to be sized.</p>
</blockquote>
<p>Right. You can think of it like this:</p>
<p>The function <em>body</em>, to type-check, requires its parameters + return-type are <code>Sized</code>, because we wouldn't know how to generate code otherwise (but if there is no body, we don't have to generate code, so no error).</p>
<p>Similarly, calling  a function imposes the same requirement, for the same reason.</p>



<a name="157301109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157301109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157301109">(Jan 31 2019 at 19:57)</a>:</h4>
<blockquote>
<p>Can the "unsized locals/rvalue" work not apply to a return type then?</p>
</blockquote>
<p>No, it can't, at least, not without more complexity. The problem is that the way return types work, the caller has to allocate stack space for the callee to write into, but without knowing the type of the value that will be returned, we can't know how much stack space to allocate.</p>



<a name="157301160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157301160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157301160">(Jan 31 2019 at 19:58)</a>:</h4>
<p>(In contrast, we know how big the parameters we're about to pass are, or we can figure it out)</p>



<a name="157301800"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157301800" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157301800">(Jan 31 2019 at 20:04)</a>:</h4>
<p>Thank you! I think I will need to do some deeper thinking as to how the actual implementation of <code>Box&lt;FnOnce()&gt;</code> can work.</p>



<a name="157302313"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157302313" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mikeyhew <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157302313">(Jan 31 2019 at 20:10)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span>  About that: I've always thought it would be possible to alloca the return value, and then move it when popping the stack frame. Has that been considered?</p>



<a name="157303415"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157303415" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mikeyhew <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157303415">(Jan 31 2019 at 20:24)</a>:</h4>
<p><a href="/user_uploads/4715/VeJvdfHCSCtp0ipp4_ETgfFE/Screen-Shot-2019-01-31-at-3.23.48-PM.png" target="_blank" title="Screen-Shot-2019-01-31-at-3.23.48-PM.png">Screen-Shot-2019-01-31-at-3.23.48-PM.png</a> Like this</p>
<div class="message_inline_image"><a href="/user_uploads/4715/VeJvdfHCSCtp0ipp4_ETgfFE/Screen-Shot-2019-01-31-at-3.23.48-PM.png" target="_blank" title="Screen-Shot-2019-01-31-at-3.23.48-PM.png"><img src="/user_uploads/4715/VeJvdfHCSCtp0ipp4_ETgfFE/Screen-Shot-2019-01-31-at-3.23.48-PM.png"></a></div>



<a name="157305474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157305474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157305474">(Jan 31 2019 at 20:54)</a>:</h4>
<p>I don't know that I'd thought about moving it, I suppose it may be possible</p>



<a name="157305479"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/144729-wg-traits/topic/questions%20from%20Stack%20Overflow/near/157305479" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/144729-wg-traits/topic/questions.20from.20Stack.20Overflow.html#157305479">(Jan 31 2019 at 20:54)</a>:</h4>
<p>certainly a big change to the ABI</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>